/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package limousine.dbservice;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.DataSource;
import limousine.util.Func;
import limousine.vo.Driver;

/**
 *
 * @author Tailer
 */
public class DriverService {

    private DataSource ds = null;
    private Func func = new Func();

    public DataSource getDs() {
        return ds;
    }

    public void setDs(DataSource ds) {
        this.ds = ds;
    }

    public Driver getByDriverId(String driverId) {
        StringBuffer sql = new StringBuffer();
        sql.append("select * From DRIVER where DRIVER_ID = '" + driverId.trim() + "' ");
        Driver o = null;
        try {
            Connection conn = ds.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql.toString());
            if (rs.next()) {
                o = new Driver();
                o.setDriverId(rs.getString("DRIVER_ID"));
                o.setDriverName(rs.getString("DRIVER_NAME"));
                o.setDriverType(rs.getInt("DRIVER_TYPE"));
                o.setDriverRate(rs.getInt("DRIVER_RATE"));
                o.setPerType(rs.getInt("PER_TYPE"));
                o.setAccountNo(rs.getString("ACCOUNT_NO"));
                o.setBank(rs.getString("BANK"));
                o.setStatus(rs.getInt("STATUS"));
                o.setCreateBy(rs.getString("CREATE_BY"));
                o.setCreateDate(rs.getTimestamp("CREATE_DATE"));
                o.setUpdateBy(rs.getString("UPDATE_BY"));
                o.setUpdateDate(rs.getTimestamp("UPDATE_DATE"));
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return o;
    }

    public boolean insert(Driver o) {
        StringBuffer sql = new StringBuffer();
        sql.append("INSERT INTO DRIVER(DRIVER_ID,DRIVER_NAME,DRIVER_TYPE,DRIVER_RATE,PER_TYPE,ACCOUNT_NO,BANK, ");
        sql.append("STATUS,CREATE_BY,CREATE_DATE,UPDATE_BY,UPDATE_DATE ) ");
        sql.append("VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
        try {
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            stmt.setString(1, o.getDriverId());
            stmt.setString(2, o.getDriverName());
            stmt.setInt(3, o.getDriverType());
            stmt.setInt(4, o.getDriverRate());
            stmt.setInt(5, o.getPerType());
            stmt.setString(6, o.getAccountNo());
            stmt.setString(7, o.getBank());
            stmt.setInt(8, o.getStatus());
            stmt.setString(9, o.getCreateBy());
            stmt.setTimestamp(10, func.getTimestamp());
            stmt.setString(11, o.getUpdateBy());
            stmt.setTimestamp(12, func.getTimestamp());
            stmt.executeUpdate();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean update(Driver o) {
        StringBuffer sql = new StringBuffer();
        sql.append("UPDATE DRIVER ");
        sql.append("SET DRIVER_NAME=?,DRIVER_TYPE=?,DRIVER_RATE=?,PER_TYPE=?,ACCOUNT_NO=?,BANK=?, ");
        sql.append("STATUS=?,UPDATE_BY=?,UPDATE_DATE=? ");
        sql.append("where DRIVER_ID='" + o.getDriverId() + "' ");
        try {
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            stmt.setString(1, o.getDriverName());
            stmt.setInt(2, o.getDriverType());
            stmt.setInt(3, o.getDriverRate());
            stmt.setInt(4, o.getPerType());
            stmt.setString(5, o.getAccountNo());
            stmt.setString(6, o.getBank());
            stmt.setInt(7, o.getStatus());
            stmt.setString(8, o.getUpdateBy());
            stmt.setTimestamp(9, func.getTimestamp());
            stmt.executeUpdate();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean deleteByTableId(String driverId) {
        StringBuffer sql = new StringBuffer();
        sql.append("delete from DRIVER where DRIVER_ID = '" + driverId.trim() + "' ");
        try {
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            stmt.executeUpdate();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean deleteBySQL(String sql) {
        try {
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.executeUpdate();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }
}
